from flask import Blueprint, jsonify
from app.services.container_service import ContainerService

container_bp = Blueprint('container', __name__)

@container_bp.route('/api/containers/list', methods=['GET'])
def get_containers():
    """
    获取容器列表
    ---
    tags:
      - 容器管理
    responses:
      200:
        description: 返回容器列表信息
        schema:
          type: object
          properties:
            message:
              type: list
              example: {
    "Create_Time": "年-月-日 00:00:00",
    "command": [],
    "id": "",
    "image": "",
    "name": "",
    "ports": {},
    "status": ""
  }
    """
    containers = ContainerService.get_all_containers()
    return jsonify(containers)




@container_bp.route('/api/containers/<container_id>/start', methods=['POST'])
def start_container(container_id):

    """
    启动指定容器
    ---
    tags:
      - 容器管理
    parameters:
      - id: container_id
        name: container_id
        in: /api/containers/<container_id>/start
        type: string
        required: true
        description: 容器的 ID
    responses:
      200:
        description: 容器启动成功
        schema:
          type: object
          properties:
            message:
              type: string
              example: Container container_id started successfully
      404:
        description: 容器未找到
        schema:
          type: object
          properties:
            message:
              type: string
              example: Container container_id not found
      500:
        description: 启动容器失败
        schema:
          type: object
          properties:
            message:
              type: string
              example: Failed to start container container_id
            error:
              type: string
              example: Error message from Docker API
    """
    result = ContainerService.start_containers(container_id)
    return result


@container_bp.route('/api/containers/<container_id>/stop', methods=['POST'])
def stop_container(container_id):
    """
        停止指定容器
        ---
        tags:
          - 容器管理
        parameters:
          - id: container_id
            name: container_id
            in: /api/containers/<container_id>/stop
            type: string
            required: true
            description: 容器的 ID
        responses:
          200:
            description: 容器停止成功
            schema:
              type: object
              properties:
                message:
                  type: string
                  example: Container container_id stoped successfully
          404:
            description: 容器未找到
            schema:
              type: object
              properties:
                message:
                  type: string
                  example: Container container_id not found
          500:
            description: 停止容器失败
            schema:
              type: object
              properties:
                message:
                  type: string
                  example: Failed to stop container container_id
                error:
                  type: string
                  example: Error message from Docker API
        """
    result = ContainerService.stop_containers(container_id)
    return result

@container_bp.route('/api/containers/<container_id>/remove', methods=['POST'])
def remove_container(container_id):
    """
            删除指定容器
            ---
            tags:
              - 容器管理
            parameters:
              - id: container_id
                name: container_id
                in: /api/containers/<container_id>/remove
                type: string
                required: true
                description: 容器ID
            responses:
              200:
                description: 容器删除成功
                schema:
                  type: object
                  properties:
                    message:
                      type: string
                      example: Container container_id remove successfully
              404:
                description: 容器未找到
                schema:
                  type: object
                  properties:
                    message:
                      type: string
                      example: Container container_id not found
              500:
                description: 删除容器失败
                schema:
                  type: object
                  properties:
                    message:
                      type: string
                      example: Failed to remove container container_id
                    error:
                      type: string
                      example: Error message from Docker API
            """
    result = ContainerService.remove_container(container_id)
    return result

@container_bp.route('/api/containers/<container_id>/logs', methods=['POST'])
def logs_container(container_id):
    """
                获取容器日志
                ---
                tags:
                  - 容器管理
                parameters:
                  - id: container_id
                    name: container_id
                    in: /api/containers/<container_id>/logs
                    type: string
                    required: true
                    description: 容器ID
                responses:
                  404:
                    description: 容器未找到
                    schema:
                      type: object
                      properties:
                        message:
                          type: string
                          example: Container container_id not found
                  500:
                    description: 获取容器日志失败
                    schema:
                      type: object
                      properties:
                        message:
                          type: string
                          example: Failed to remove container container_id
                        error:
                          type: string
                          example: Error message from Docker API
                """
    result = ContainerService.logs_container(container_id)
    return result

@container_bp.route('/api/containers/create', methods=['POST'])
def create_container():
    """
        创建容器
        ---
        tags:
        - 容器管理
        parameters:
        - id: image_name
          name: image_name
          in: /api/containers/create
          type: string
          required: true
          description: 镜像名称
        - id: container_name
          name: container_name
          in: /api/containers/create
          type: string
          required: true
          description: 容器名称
        - id: host_port
          name: host_port
          in: /api/containers/create
          type: string
          required: false
          description: 主机端口
        - id: container_port
          name: container_port
          in: /api/containers/create
          type: string
          required: false
          description: 容器端口
        - id: host_volume
          name: host_volume
          in: /api/containers/create
          type: string
          required: false
          description: 主机路径
        - id: container_volume
          name: container_volume
          in: /api/containers/create
          type: string
          required: false
          description: 容器路径
        responses:
            200:
                description: 容器创建成功
                schema:
                    type: object
                    properties:
                    message:
                    type: string
                    example: Container container_id create successfully
            500:
                description: 容器创建失败
                schema:
                    type: object
                    properties:
                      message:
                        type: string
                        example: Error message from Docker API
                      error:
                        type: string
                        example: Error message from Docker API
    """

    result = ContainerService.create_container()
    return result